﻿using strange.extensions.command.impl;
using strange.extensions.context.api;
using strange.extensions.context.impl;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DealCommand : EventCommand {
    [Inject]
    public CardModel cardModel { get; set; }

    [Inject(ContextKeys.CONTEXT_VIEW)]//得到挂有ConTextView的物体
    public GameObject GameRoot { get; set; }
    /// <summary>
    /// 执行命令
    /// </summary>
    public override void Execute()
    {
        cardModel.Shuffle();
        //发牌
        GameRoot.GetComponent<GameRoot>().StartCoroutine(DealCard());
    }

    /// <summary>
    /// 发牌
    /// </summary>
    /// <returns></returns>
    IEnumerator DealCard()
    {
        CharacterType Current = CharacterType.Player;
        for (int i = 0; i < 51; i++)
        {
            if (Current == CharacterType.Desk)
            {
                Current = CharacterType.Player;
            }
            DealTo(Current);
            //换人
            Current++; 
            //等待   
            yield return new WaitForSeconds(0.05f);
        }
        //发底牌
        for (int i = 0; i < 3; i++)
        {
            DealTo(CharacterType.Desk);
        }
        //排序后发一个消息标志发牌结束
        dispatcher.Dispatch(ViewEvent.CompeletDeal);
    }
    /// <summary>
    /// 发牌
    /// </summary>
    /// <param name="cType">给谁</param>
    public void DealTo(CharacterType cType)
    {
        Card card = cardModel.DealCard(cType);

        DealCardAgrs e = new DealCardAgrs(card, cType, false);

        dispatcher.Dispatch(CommandEvent.Command_DealCard, e);

    }
}
